# -*- coding: utf-8 -*-

import akshare as ak
import numpy as np
import pandas as pd

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.float_format', '{:,}'.format)
pd.set_option('expand_frame_repr', False)


class StockDataFetch:
    def __int__(self, stock_code):
        self.stock_code = stock_code

    def read(self, startDate, endDate):
        """
        read stock-data from api
        :param startDate:   stock-data fetch start_date
        :param endDate: stock-data fetch end_date
        :return: stock_data dataset
        """
        if endDate is not None:
            stock_his_data = ak.stock_zh_a_hist(symbol=self.stock_code, period="daily", start_date=startDate,
                                                end_date=endDate, adjust="qfq", timeout=50000)
        else:
            stock_his_data = ak.stock_zh_a_hist(symbol=self.stock_code, period="daily", start_date=startDate,
                                                djust="qfq", timeout=50000)
        if stock_his_data is not None:
            # sort by date
            stock_his_data = stock_his_data.sort_values("日期")
            # create other data_frame
            stock_data = pd.DataFrame()
            stock_data["date"] = stock_his_data["日期"]
            stock_data["open"] = stock_his_data["开盘"]
            stock_data["close"] = stock_his_data["收盘"]
            stock_data["high"] = stock_his_data["最高"]
            stock_data["low"] = stock_his_data["最低"]
            stock_data["vol"] = stock_his_data["成交量"]
            stock_data["vol_amount"] = stock_his_data["成交额"]
            stock_data["change"] = stock_his_data["换手率"]
            stock_data["increment"] = stock_his_data["涨跌幅"]
            # stock_data_new_df
            stock_data_new = pd.DataFrame(stock_data)
            stock_data_new["zz"] = (stock_data_new["open"] + stock_data_new["low"] + stock_data_new["high"]) / 3
            stock_data_new["zl1"] = stock_data_new["zz"].ewm(span=7, adjust=True).mean().round(2)
            stock_data_new["zl2"] = stock_data_new["zz"].ewm(span=55, adjust=True).mean().round(2)
            stock_data_new = stock_data_new.drop('zz', axis=1)
            # next days max value
            stock_data_new["next_3_max_value"] = np.zeros(len(stock_data_new))
            stock_data_new["next_5_max_value"] = np.zeros(len(stock_data_new))
            stock_data_new["result"] = np.zeros(len(stock_data_new))
            return stock_data_new
        else:
            return None

    def value_fill(self,stock_data):
        for index in range(0, len(stock_data)):
            next_3_value = stock_data.loc[index + 1:index + 3]["high"].max()
            next_5_value = stock_data.loc[index + 1:index + 5]["high"].max()
            stock_data.iloc[index, 11] = next_3_value
            stock_data.iloc[index, 12] = next_5_value
            current_close_value = stock_data.loc[index]["close"]
            if next_3_value > current_close_value and (next_3_value - current_close_value) / current_close_value >= 0.20:
                stock_data.iloc[index, 13] = 10








            current_close_value = stock_data.loc[index]["close"]
            if (next_value - current_close_value) / current_close_value > inc_percent5:
                stock_data_new.iloc[index, 11] = 5
            elif (next_value - current_close_value) / current_close_value > inc_percent4:
                stock_data_new.iloc[index, 11] = 4
            elif (next_value - current_close_value) / current_close_value > inc_percent3:
                stock_data_new.iloc[index, 11] = 3
            elif (next_value - current_close_value) / current_close_value > inc_percent2:
                stock_data_new.iloc[index, 11] = 2
            elif (next_value - current_close_value) / current_close_value > inc_percent1:
                stock_data_new.iloc[index, 11] = 1
            elif next_value >= current_close_value:
                stock_data_new.iloc[index, 11] = 0.5
            else:
                if (current_close_value - next_value) / current_close_value > inc_percent5:
                    stock_data_new.iloc[index, 11] = -5
                elif (current_close_value - next_value) / current_close_value > inc_percent4:
                    stock_data_new.iloc[index, 11] = -4
                elif (current_close_value - next_value) / current_close_value > inc_percent3:
                    stock_data_new.iloc[index, 11] = -3
                elif (current_close_value - next_value) / current_close_value > inc_percent2:
                    stock_data_new.iloc[index, 11] = -2
                elif (current_close_value - next_value) / current_close_value > inc_percent1:
                    stock_data_new.iloc[index, 11] = -1
                elif current_close_value > next_value:
                    stock_data_new.iloc[index, 11] = -0.5

